package app.lockenabler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

public class Settings extends Activity {

	private int timeout;
	
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        if (Options.getDevice().equalsIgnoreCase("HTC Desire")){
        	Options.setSettingsInt("Lock_Screen_Time_Out", 5, Settings.this);
        	SeekBar TO = (SeekBar)findViewById(R.id.timeout);
        	TO.setEnabled(false);
        	TextView WRNG = (TextView)findViewById(R.id.warning);
 	   		WRNG.setVisibility(View.VISIBLE);
 	   		WRNG.setText(getString(R.string.warning_desire));
        }
        
        TextView VWTM = (TextView)findViewById(R.id.viewtimeout);
        VWTM.setText(""+(Options.getSettingsInt("Lock_Screen_Time_Out", 2, Settings.this)+1)+" "+getString(R.string.seconds));
        
        CheckBox VBRA = (CheckBox)findViewById(R.id.vibra);
 		if(Options.getSettingsBoolean("Vibration_Notification", true, Settings.this)){
 			VBRA.setChecked(true);
 		}
 		else{
 			VBRA.setChecked(false);
 		}
 		
 		CheckBox EXT = (CheckBox)findViewById(R.id.touchexit);
 		if(Options.getSettingsBoolean("Exit_On_Touch", false, Settings.this)){
 			EXT.setChecked(true);
 		}
 		else{
 			EXT.setChecked(false);
 		}

        SeekBar TO = (SeekBar)findViewById(R.id.timeout);
        TO.setProgress(Options.getSettingsInt("Lock_Screen_Time_Out", 2, Settings.this));
        TO.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
               	TextView VIEWTIM = (TextView)findViewById(R.id.viewtimeout);
            	VIEWTIM.setText(""+(progress+1)+" "+getString(R.string.seconds));
         	   	timeout = progress;
         	   	if (timeout == 0){
         	   		Options.setSettingsInt("Lock_Screen_Time_Out", 0, Settings.this);
         	   		TextView WRNG = (TextView)findViewById(R.id.warning);
         	   		WRNG.setVisibility(View.VISIBLE);
         	   		WRNG.setText(getString(R.string.warning));
         	   	}
         	   	else{
         	   		Options.setSettingsInt("Lock_Screen_Time_Out", timeout, Settings.this);
         	   		TextView WRNG = (TextView)findViewById(R.id.warning);
         	   		WRNG.setVisibility(View.GONE);
         	   		WRNG.setText("");
         	   	}
            }
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        
        Button save = (Button) findViewById(R.id.savesettings);
        save.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
        	   if (timeout != 0){
        		   Options.setSettingsInt("Lock_Screen_Time_Out", timeout, Settings.this);
        		   finish();
        	   }
        	   
        	   CheckBox VBRA = (CheckBox) findViewById(R.id.vibra);
        	   if(VBRA.isChecked()){
        		   Options.setSettingsBoolean("Vibration_Notification", true, Settings.this);
        	   }
        	   else{
        		   Options.setSettingsBoolean("Vibration_Notification", false, Settings.this);
        	   }
        	   
    		   CheckBox EXT = (CheckBox) findViewById(R.id.touchexit);
        	   if(EXT.isChecked()){
        		   Options.setSettingsBoolean("Exit_On_Touch", true, Settings.this);
        	   }
        	   else{
        		   Options.setSettingsBoolean("Exit_On_Touch", false, Settings.this);
        	   }
        	   finish();
           }
        });
        
        ImageButton DNT = (ImageButton) findViewById(R.id.donate_button);
        
        DNT.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            	Intent i = new Intent(Settings.this, Donate.class);
		        startActivity(i);
            }
        });
    }
	
	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
}
